<template>
  <!-- //v-for 不能添加到根元素上 -->
  <ul class="list">
    <li
      class="item"
      v-for="item in letters"
      :key="item"
      @click="handleLetter"
      @touchstart.prevent="handleTouchStart"
      @touchmove="handleTouchMove"
      @touchend="handleTouchEnd"
      :ref="item"
    >{{item}}</li>
  </ul>
</template>
<script>
export default {
  props: {
    cities: Object
  },
  data() {
    return {
      touchStatus: false,
      startY: 0,
      timer: null
    };
  },
  updated() {
    this.startY = this.$refs["A"][0].offsetTop;
  },
  computed: {
    letters() {
      var letters = [];
      for (var key in this.cities) {
        letters.push(key);
      }
      return letters;
    }
  },
  methods: {
    handleLetter(e) {
      this.$emit("change", e.target.innerText);
    },
    handleTouchStart() {
      this.touchStatus = true;
    },
    handleTouchMove(e) {
      if (this.timer) {
        clearTimeout(this.timer);
      } else {
        setTimeout(() => {
          if (this.touchStatus) {
            var touchY = e.touches[0].clientY - 79;
            var index = Math.floor((touchY - this.startY) / 22);
            this.$emit("change", this.letters[index]);
          }
        }, 16);
      }
    },
    handleTouchEnd() {
      this.touchStatus = false;
    }
  }
};
</script>
<style lang="stylus" scoped>
@import '~@/assets/style/varibles.styl';

.list {
  display: flex;
  flex-direction: column;
  justify-content: center;
  position: absolute;
  top: 1.58rem;
  right: 0;
  bottom: 0;
  width: 0.4rem;
  text-align: center;

  // background #cccccc
  .item {
    line-height: 0.44rem;
    color: $bgColor;
  }
}
</style>
